package pro.ticktock.tools.mbconv;

import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedList;

/**
 * Created by lambert on 2017/5/28.
 */
public class Pass5 extends AbstractPass{
  protected Pass5(String origSrcFileName, String srcFileName) throws IOException {
    super(origSrcFileName, srcFileName);
  }

  String openBrackets  = "【[<(（《〈≮";
  String closeBrackets = "】]>)）》〉≯";
  LinkedList<Character> openBracketStack = new LinkedList<>();
  LinkedList<Integer> openBracketIndex = new LinkedList<>();

  @Override
  protected void doProcess() throws IOException {
    String line;
    while ((line = srcReader.readLine()) != null) {
      openBracketStack.clear();
      openBracketIndex.clear();
      char[] chs = line.toCharArray();

      for (int i = 0; i < chs.length; i++) {
        if (openBrackets.indexOf(chs[i]) >= 0) {
          openBracketStack.push(chs[i]);
          openBracketIndex.push(i);
        } else {
          int matchJ = closeBrackets.indexOf(chs[i]);
          if (matchJ >= 0) {
            if (openBracketStack.peek() == null) {
              logWriter.append("Unexpected close bracket in line: " + line + "\n");
            } else {
              char lastOpen = openBracketStack.pop();
              int startIdx = openBracketIndex.pop();
              int j = openBrackets.indexOf(lastOpen);
              if (matchJ == j) {
                if (chs[startIdx] == '<' || chs[startIdx] == '〈' || chs[startIdx] == '≮' ) {
                  chs[startIdx] = '《';
                  chs[i] = '》';
                } else if (chs[startIdx] == '(') {
                  chs[startIdx] = '（';
                  chs[i] = '）';
                }
              } else {
                logWriter.append("Not matched: " + new String(Arrays.copyOfRange(chs, startIdx, i+1)) + "  ->open: '" + lastOpen + "', close: '" + chs[i] + "'\n");
                char start = chs[startIdx];
                char end = chs[i];
                if (start == '【' || start == '<' || start == '≮') {
                  start = chs[startIdx] = '《';
                }
                if (end == '】' || end == '>'|| end == '≯') {
                  end = chs[i] = '》';
                }
                if (start == '《') {
                  chs[i] = '》';
                }
                if (end == '》') {
                  chs[startIdx] = '《';
                }

                if ((start == '(' && end == '）') || (start == '（' && end == ')')) {
                  start = chs[startIdx] = '（';
                  end = chs[i] = '）';
                }

                matchJ = closeBrackets.indexOf(chs[i]);
                j = openBrackets.indexOf(chs[startIdx]);
                if (matchJ != j) {
                  logWriter.append("Still not matched after 1st round: " + new String(Arrays.copyOfRange(chs, startIdx, i+1)) + "  ->open: '" + lastOpen + "', close: '" + chs[i] + "'\n");

                }
              }
            }
          }
        }
      }

      //replace between ',' and '，' , '.' and ' .' when necessary "[\u4e00-\u9fa5]"
      //for (int i = 1; i < chs.length; i++) {
      //
      //}


      resultWriter.write(new String(chs) + "\n");
    }
  }
}
